package cn.rengy.web.framework.wxmp.message;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.rengy.tpa.wxmp.token.AccessToken;
import cn.rengy.tpa.wxmp.miniprogram.message.CustomerMessageApi;
import cn.rengy.tpa.wxmp.miniprogram.message.CustomerMessageApi.Msgtype;
import cn.rengy.tpa.wxmp.miniprogram.message.entity.CustomerMessageRequest;
import cn.rengy.tpa.wxmp.miniprogram.message.TemplateMessageApi;
import cn.rengy.tpa.wxmp.miniprogram.message.entity.TemplateMessageRequest;
import cn.rengy.tpa.wxmp.miniprogram.message.entity.TextMessage;
import cn.rengy.tpa.wxmp.miniprogram.message.entity.WeappTemplateMsgData;
import cn.rengy.web.framework.cache.CacheService;
import cn.rengy.web.framework.sql.DbService;
import cn.rengy.web.framework.wxmp.token.AccessTokenCacheService;

@Service
public class WXAppMessageService {
	@Autowired
	private DbService dbService;
	
	@Autowired
	private CacheService cacheService;
	
	@Autowired
	private AccessTokenCacheService accessTokenCacheService;
	public void templateMsg(Map<String,Object> paramMap){
		String appid=(String)paramMap.get("appid");
		String openid=(String)paramMap.get("openid");
		String page=(String)paramMap.get("page");
		String form_id=(String)paramMap.get("form_id");
		String template_id="wThxx7zJ4_CyTSGbdbR-m5sSXQkC8iziirR1PNgGJs4";
		TemplateMessageRequest templateMessageRequest=new TemplateMessageRequest();
		templateMessageRequest.setTouser(openid);
		templateMessageRequest.setTemplate_id(template_id);
		templateMessageRequest.setPage(page);
		templateMessageRequest.setForm_id(form_id);
		List<WeappTemplateMsgData> data=new ArrayList<WeappTemplateMsgData>();
		WeappTemplateMsgData keyword1=new WeappTemplateMsgData("keyword1","value1",true);
		WeappTemplateMsgData keyword2=new WeappTemplateMsgData("keyword2","value2");
		WeappTemplateMsgData keyword3=new WeappTemplateMsgData("keyword3","value3");
		WeappTemplateMsgData keyword4=new WeappTemplateMsgData("keyword4","value4");
		data.add(keyword1);
		data.add(keyword2);
		data.add(keyword3);
		data.add(keyword4);
		templateMessageRequest.setData(data);
		AccessToken accessToken=accessTokenCacheService.getAccessToken(appid);
		TemplateMessageApi.sendTemplateMessage(accessToken, templateMessageRequest);
	}
	
	public void customerMsg(Map<String,Object> paramMap) {
		String appid=(String)paramMap.get("appid");
		String openid=(String)paramMap.get("openid");
		String content=(String)paramMap.get("content");
		CustomerMessageRequest customerMessage=new CustomerMessageRequest();
		customerMessage.setTouser(openid);
		TextMessage textMessage=new TextMessage();
		textMessage.setContent(content);
		customerMessage.setText(textMessage);
		customerMessage.setMsgtype(Msgtype.text);
		AccessToken accessToken=accessTokenCacheService.getAccessToken(appid);
		CustomerMessageApi.sendCustomerMessage(accessToken, customerMessage);
	}
}
